package com.scs.application.core.global;

import com.scs.application.core.utils.SpringUtils;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description: 字典工具类，提供给全局使用
 */
@Slf4j
@Service
public class GlobalDictService implements InitializingBean {

    private final ConcurrentHashMap<String, String> LOCAL_LABEL_CACHE = new ConcurrentHashMap();
    private final ConcurrentHashMap<String, Set<Dict>> LOCAL_CACHE = new ConcurrentHashMap();

    public void putDict(String type, String value, String label) {
        LOCAL_LABEL_CACHE.put(type + "-" + value, label);
        putToLocalCache(type, value, label);
    }

    private void putToLocalCache(String type, String value, String label) {
        Dict dict = new Dict(value, label);
        Set<Dict> dicts = LOCAL_CACHE.get(type);
        if (dicts == null) {
            dicts = new HashSet<>();
        }
        dicts.add(dict);
        LOCAL_CACHE.put(type, dicts);
    }

    public String getDictLabel(String type, String value, String defaultLabel) {
        String label = LOCAL_LABEL_CACHE.get(type + "-" + value);
        if (label != null) {
            return label;
        }
        if (StringUtils.isNotBlank(type) && StringUtils.isNotBlank(value)) {
            synchronized (type.intern()) {
                label = LOCAL_LABEL_CACHE.get(type + "-" + value);
                if (label != null) {
                    return label;
                }
            }

        }
        return defaultLabel;
    }

    public String getDictLabel(String type, String value) {
        return this.getDictLabel(type, value, null);
    }

    public String getDictLabels(String type, String values, String defaultValue) {
        if (StringUtils.isNotBlank(type) && StringUtils.isNotBlank(values)) {
            List<String> valueList = Lists.newArrayList();
            for (String value : StringUtils.split(values, ",")) {
                valueList.add(getDictLabel(value, type, defaultValue));
            }
            return StringUtils.join(valueList, ",");
        }
        return defaultValue;
    }

    public String getDictLabels(String type, String defaultValue) {
        List<String> valueList = Lists.newArrayList();
        Set<Dict> dicts = LOCAL_CACHE.get(type);
        if (dicts == null) {
            return defaultValue;
        }
        for (Dict dict : dicts) {
            valueList.add(dict.getLabel());
        }

        return StringUtils.join(valueList, ",");
    }


    public String getDictValue(String type, String label, String defaultValue) {
        if (StringUtils.isBlank(type) || StringUtils.isBlank(label)) {
            return defaultValue;
        }
        Set<Dict> dicts = LOCAL_CACHE.get(type);
        if (dicts != null) {
            for (Dict dict : dicts) {
                if (label.equalsIgnoreCase(dict.getLabel())) {
                    return dict.getValue();
                }
            }
        }
        return defaultValue;
    }

    public void clearAll() {
        LOCAL_LABEL_CACHE.clear();
        LOCAL_CACHE.clear();
    }


    @Override
    public void afterPropertiesSet() throws Exception {


    }

    @Data
    @AllArgsConstructor
    @EqualsAndHashCode
    private class Dict {
        private String value;
        private String label;
    }

    public static GlobalDictService getInstance() {
        try {
            GlobalDictService instance = SpringUtils.getBean(GlobalDictService.class);
            return instance;
        } catch (Exception e) {
            return null;
        }
    }
}

